package com.example.js_interaction.localweb;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.js_interaction.MainActivity;
import com.example.js_interaction.R;

import java.util.Arrays;

public class LocalWebActivity extends AppCompatActivity {
    //创建内核浏览器对象
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_local_web);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        //对象获取ID
        webView = findViewById(R.id.local_Web_id);
        //配置webView浏览器
        WebSettings settings = webView.getSettings();
        //是否支持响应JS
        settings.setJavaScriptEnabled(true);
        //android与js是否可以进行交互
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        //设置加载显示的网页
        webView.loadUrl("file:///android_asset/html/myjs.html");
        //用setWebViewClient类 复写shouldOverrideUrlLoading  使得网页调用webView浏览器
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl("file:///android_asset/html/myjs.html");
                return false;
            }
        });
        //处理JS的对话框网站网站标题等
        webView.setWebChromeClient(new WebChromeClient(){
            //重写警告框
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                new AlertDialog.Builder(LocalWebActivity.this)
                        .setTitle("niunai")
                        .setMessage(message)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.confirm();
                            }
                        })
                        .setCancelable(false)
                        .show();
                return true;
            }
            //重写确认框
            public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
                new AlertDialog.Builder(LocalWebActivity.this)
                        .setTitle("niunai")
                        .setMessage(message)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.confirm();
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.cancel();
                            }
                        })
                        .setCancelable(false)
                        .show();
                // 返回布尔值：判断点击时确认还是取消
                // true表示点击了确认；false表示点击了取消；
                return true;
            }
            //重写输入框
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
                final EditText et = new EditText(LocalWebActivity.this);
                et.setText(defaultValue);
                new AlertDialog.Builder(LocalWebActivity.this)
                        .setTitle(message)
                        .setView(et)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.confirm(et.getText().toString());
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.cancel();
                            }
                        })
                        .setCancelable(false)
                        .show();

                return true;
            }

        });
        //设置在js当中调用Android当中的函数
        webView.addJavascriptInterface(new LocalJavaScript(this),"LocalJavaScript");
    }

    /**
     * 按钮对象点击事件
     * @param view
     */
    public void onClick(View view) {
        /**
         * 在Android当中调用js函数
         * 数字可以直接传递
         * 注意如果传递的参数是String 需要加  \“变量\”
         * 数组需要   Arrays.toString(变量) 将变量转换字符串传递
         */
        int aaa = 1213;
        webView.loadUrl("javascript:musicianship(" + aaa +  ");");
        String bbb = "字符串";
        webView.loadUrl("javascript:musicianship(\"" + bbb +  "\");");
        String[] ccc = {"字符串","字符串"};
        webView.loadUrl("javascript:musicianship(\"" + Arrays.toString(ccc) +  "\");");
    }

    /**
     * 函数复写
     * 在销毁活动之前执行任何最终清理
     */
    @Override
    protected void onDestroy() {
        webView.removeView(webView);
        super.onDestroy();
    }
}